package com.ff.xyh.access.service.impl.access;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.access.service.access.AccessUserRoleService;
import com.ff.xyh.common.entity.useraccess.AccessRole;
import com.ff.xyh.access.mapper.AccessRoleMapper;
import com.ff.xyh.access.service.access.AccessRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ff.xyh.common.entity.useraccess.AccessUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台角色表 服务实现类
 * </p>
 *
 * @author ff
 * @since 2022-10-25
 */
@Service
public class AccessRoleServiceImpl extends ServiceImpl<AccessRoleMapper, AccessRole> implements AccessRoleService {

    @Autowired
    private AccessUserRoleService accessUserRoleService;

    @Override
    public List<AccessRole> getRolesByUserId(String userId) {
        // 根据用户id拥有的角色id
        List<AccessUserRole> userRoleList = accessUserRoleService.list(
                new QueryWrapper<AccessUserRole>()
                .eq("user_id", userId));
        // 获取roleId列表
        List<String> roleIdList = userRoleList.stream()
                .map(item -> item.getRoleId()).collect(Collectors.toList());
        if(roleIdList == null || roleIdList.isEmpty()) {
            return null;
        } else {
            List<AccessRole> roleList = baseMapper.selectBatchIds(roleIdList);
            if (roleList == null || roleList.isEmpty()) {
                return null;
            } else {
                return roleList;
            }
        }
    }
}
